#include "StdAfx.h"
#include "ServerConfig.h"

CServerConfig::CServerConfig(void)
{
}

CServerConfig::~CServerConfig(void)
{
}

std::string CServerConfig::getConfigFileName(void)
{
	return getRootFolder()+std::string("\\TinyCC\\localhost\\CCServer.xml");
}

bool CServerConfig::readConfigFile(std::string filename)
{
	if(openXmlFile(filename) == false)
		return false;
	try{
		xercesc::DOMNode *current2 = m_walker->getCurrentNode();
		//current2=walker->nextNode();
		std::string name(xercesc::XMLString::transcode(current2->getNodeName()));

		if(name=="CCServer")
		{

			for(current2=m_walker->nextNode();current2!=0;current2=m_walker->nextNode())
			{
				//read setting
				name = xercesc::XMLString::transcode(current2->getNodeName());
				if(name==std::string("Instance"))
				{

					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "Name")
						this->m_InstanceName = xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());

					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "ModuleIP")
						m_ModuleIP = xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());

					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "ModulePort")
						m_ModulePort = atoi(xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent()));

					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "DataBaseConnStr")
						m_DataBaseConnStr = xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());

				}
				name = xercesc::XMLString::transcode(current2->getNodeName());
				if(name == std::string("Debug"))
				{
					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "logip")
						this->m_logip = xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());

					current2=m_walker->nextNode();
					if(current2==0)
						break;
					name = xercesc::XMLString::transcode(current2->getNodeName());
					if(name == "logport")
						this->m_logport = atoi(xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent()));
				}
				/*if(name==std::string("author"))
				{
				tempData->author =xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());
				//wait to finish
				}
				if(name==std::string("date"))
				{
				tempData->date =xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());
				//wait to finish
				}
				if(name==std::string("path"))
				{
				//logmsg =xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());
				//wait to finish
				std::string filepath=xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());
				std::string filename = filepath.substr(filepath.find_last_of("/")+1,filepath.length());
				tempData->filelist.push_back(std::pair<std::string,std::string>(filename,filepath));

				}
				if(name==std::string("msg"))
				{
				tempData->logmsg =xercesc::XMLString::transcode(((xercesc::DOMElement*)current2)->getTextContent());
				//wait to finish
				m_RevisionList.push_back(*tempData);
				//clear tempData;
				tempData->author="";
				tempData->date="";
				tempData->logmsg="";
				tempData->revision="";
				tempData->filelist.clear();

				}*/

			}
		}
	}
	catch(...)
	{
		//		strTime = CTime::GetCurrentTime().Format("%Y-%m-%d-%a  %H:%M:%S"); 
		//MessageBox ("Parse xml file Error");
		closeXmLFile();
		return false;
	}
	//delete errHandler;
	//end read version xml file
	closeXmLFile();
	return true;
}